{smcl}
{com}{sf}{ul off}{txt}{.-}
      name:  {res}<unnamed>
       {txt}log:  {res}R:\WSV2\TBu_AKe\Spatial_NEW\spatial_2_collapse_s.smcl
  {txt}log type:  {res}smcl
 {txt}opened on:  {res}28 Mar 2023, 16:20:32
{txt}
{com}. cd // display in log file 
{res}R:\WSV2\TBu_AKe\Spatial_NEW
{txt}
{com}. 
. ** load data from spatial_1_raw 
. use spatial_1_raw_s, replace  // slow when loaded from server
{txt}
{com}. 
. 
. *************************************
. ** Part 1: Product Space Set-up *****
. *************************************
. 
.   ** 1: generate cell boundaries
. 
. scalar kappa = 51.7*0.59 // intercept at compliance line 
{txt}
{com}. scalar sigma = 47*0.59 // attribute slope at compliance line 
{txt}
{com}. scalar D = 3 // interval
{txt}
{com}. 
.         ** gen identifying points 
. gen a_min = a_bin - 0.25 // left boundary 
{txt}(200 missing values generated)

{com}. gen a_max = a_bin + 0.25 // right boundary 
{txt}(200 missing values generated)

{com}. gen k_bin = s_bin + `=kappa' // distance to compliance line
{txt}(26 missing values generated)

{com}.         ** 
. gen e_low_left = k_bin + a_min*`=sigma'
{txt}(225 missing values generated)

{com}. gen e_up_left = e_low_left + `=D'
{txt}(225 missing values generated)

{com}. gen e_low_right = e_low_left + 0.5*`=sigma'
{txt}(225 missing values generated)

{com}. gen e_up_right = e_low_left + `=D' + 0.5*`=sigma'
{txt}(225 missing values generated)

{com}. 
.         ** test a cell. 
. sum e_low* e_up* if a_bin == 6 & s_bin == 0 // 189, 192 at left - 203, 206 at right 

{txt}    Variable {c |}        Obs        Mean    Std. dev.       Min        Max
{hline 13}{c +}{hline 57}
{space 2}e_low_left {c |}{res}      4,204    189.9505           0   189.9505   189.9505
{txt}{space 1}e_low_right {c |}{res}      4,204    203.8155           0   203.8155   203.8155
{txt}{space 3}e_up_left {c |}{res}      4,204    192.9505           0   192.9505   192.9505
{txt}{space 2}e_up_right {c |}{res}      4,204    206.8155           0   206.8155   206.8155
{txt}
{com}. 
.         ** label as safety net 
. gen e_bin = e_low_left 
{txt}(225 missing values generated)

{com}. label variable e_bin "E_i low-left"
{txt}
{com}. label variable a_min "a_i left"
{txt}
{com}. label variable a_max "a_i right"
{txt}
{com}. label variable k_bin "kappa (std. E)"
{txt}
{com}. 
. *****************************************************
.         ** 2a: fixes needed to get loops to work **
. drop if a_bin == . // missing ids. 
{txt}(200 observations deleted)

{com}. drop if e_bin == .  // 
{txt}(25 observations deleted)

{com}. drop if s_bin < -252 // censoring at lowest value 
{txt}(87 observations deleted)

{com}. replace added_a = 1 if added_a == . // 1 if added cell, 0 otherwise. 
{txt}(4,597 real changes made)

{com}. replace added_s = 1 if added_s == . // 1 if added cell, 0 otherwise. 
{txt}(4,597 real changes made)

{com}. 
.         
. egen cell=group(e_bin a_bin) // rectangles with 3 kwh and 0.5 kg 
{txt}
{com}. codebook cell 

{txt}{hline}
{res}cell{right:group(e_bin a_bin)}
{txt}{hline}

{col 19}Type: Numeric ({res}float{txt})

{col 18}Range: [{res}1{txt},{res}4950{txt}]{col 55}Units: {res}1
{col 10}{txt}Unique values: {res}4,950{col 51}{txt}Missing .: {res}0{txt}/{res}504,599

{txt}{col 19}Mean: {res}{ralign 7:1292.84}
{txt}{col 14}Std. dev.: {res}{ralign 7:318.985}

{txt}{col 12}Percentiles: {col 25}{ralign 7:10%}{ralign 10:25%}{ralign 10:50%}{ralign 10:75%}{ralign 10:90%}
{res}{col 25}{ralign 7:1030}{ralign 10:    1143}{ralign 10:    1234}{ralign 10:    1395}{ralign 10:    1582}
{txt}
{com}.         * result: 4950 (198*25) 
.         
. codebook e_bin // 4950

{txt}{hline}
{res}e_bin{right:E_i low-left}
{txt}{hline}

{col 19}Type: Numeric ({res}float{txt})

{col 18}Range: [{res}-145.23949{txt},{res}778.52051{txt}]{col 55}Units: {res}1.000e-08
{col 10}{txt}Unique values: {res}4,950{col 51}{txt}Missing .: {res}0{txt}/{res}504,599

{txt}{col 19}Mean: {res}{ralign 7:173.089}
{txt}{col 14}Std. dev.: {res}{ralign 7:40.8671}

{txt}{col 12}Percentiles: {col 25}{ralign 7:10%}{ralign 10:25%}{ralign 10:50%}{ralign 10:75%}{ralign 10:90%}
{res}{col 25}{ralign 7:138.951}{ralign 10:  154.68}{ralign 10:  166.68}{ralign 10: 186.951}{ralign 10: 209.411}
{txt}
{com}. codebook a_bin // 25. 

{txt}{hline}
{res}a_bin{right:(unlabeled)}
{txt}{hline}

{col 19}Type: Numeric ({res}float{txt})

{col 18}Range: [{res}3{txt},{res}15{txt}]{col 55}Units: {res}.1
{col 10}{txt}Unique values: {res}25{col 51}{txt}Missing .: {res}0{txt}/{res}504,599

{txt}{col 19}Mean: {res}{ralign 7:6.67118}
{txt}{col 14}Std. dev.: {res}{ralign 7:1.30213}

{txt}{col 12}Percentiles: {col 25}{ralign 7:10%}{ralign 10:25%}{ralign 10:50%}{ralign 10:75%}{ralign 10:90%}
{res}{col 25}{ralign 7:5}{ralign 10:       6}{ralign 10:       7}{ralign 10:       7}{ralign 10:       8}
{txt}
{com}. *scatter s_bin a_bin, msize(small) msymbol(Dh) // checkpoint: grid is regular. 
. 
.         
. ******** define segments in product space ***********
. 
. gen r = 0 // restricted
{txt}
{com}. gen b = 0 // bunching 
{txt}
{com}. gen u = 0 // unrestricted
{txt}
{com}. 
. replace r = 1 if eei_bin >= 59 
{txt}(73,795 real changes made)

{com}. replace b = 1 if eei_bin < 59 & eei_bin >= 57 // 2-unit eei window : 69k obs. 
{txt}(69,173 real changes made)

{com}. replace u = 1 if eei_bin < 57     
{txt}(361,631 real changes made)

{com}. 
. *********************************************
.         ** 2b: outcome by cell: country-specific *      
.         
.                 ************
.                 ** yearly **
.                 ************            
.         ** cell-level sales, country specific
. by cell ccode, sort: egen cell_sales=sum(units) // all years 
{txt}
{com}. by cell year ccode, sort: egen cell_year_sales=sum(units) // by year 
{txt}
{com}. 
.         ** cell-level product count 
. by cell year ccode id,  sort: gen n=_n  
{txt}
{com}. replace n=0                     if n>1 &n!=.  
{txt}(443,788 real changes made)

{com}. replace n=0 if added_a == 1
{txt}(4,597 real changes made)

{com}. replace n=0 if added_s == 1 
{txt}(0 real changes made)

{com}. by cell year ccode,             sort: egen cell_year_count=sum(n) // by year 
{txt}
{com}. drop n  
{txt}
{com}.         
. by cell ccode id,       sort: gen n=_n  
{txt}
{com}. replace n=0                     if n>1 &n!=. 
{txt}(485,608 real changes made)

{com}.         *cell-level product count 
. replace n=0 if added_a == 1
{txt}(4,597 real changes made)

{com}. replace n=0 if added_s == 1 
{txt}(0 real changes made)

{com}. by cell ccode,          sort: egen cell_count=sum(n) // all years 
{txt}
{com}. drop n 
{txt}
{com}. 
. 
.         ** cell-level sales in R and B  
. by cell year ccode, sort: egen cell_year_sales_R=sum(units) if r== 1 // by year 
{txt}(430,804 missing values generated)

{com}. by cell year ccode, sort: egen cell_year_sales_B=sum(units) if b == 1 // by year 
{txt}(435,426 missing values generated)

{com}. 
.         ** cell-level product count 
. by cell year ccode id,  sort: gen n=_n  
{txt}
{com}. replace n=0                     if n>1 &n!=.  
{txt}(443,788 real changes made)

{com}. replace n=0 if added_a == 1
{txt}(4,597 real changes made)

{com}. replace n=0 if added_s == 1 
{txt}(0 real changes made)

{com}. replace n=0 if r == 0 
{txt}(48,840 real changes made)

{com}. by cell year ccode,             sort: egen cell_year_count_R =sum(n) // by year 
{txt}
{com}. drop n  
{txt}
{com}.         
. by cell year ccode id,  sort: gen n=_n  
{txt}
{com}. replace n=0                     if n>1 &n!=.  
{txt}(443,788 real changes made)

{com}. replace n=0 if added_a == 1
{txt}(4,597 real changes made)

{com}. replace n=0 if added_s == 1 
{txt}(0 real changes made)

{com}. replace n=0 if b == 0 
{txt}(48,645 real changes made)

{com}. by cell year ccode,             sort: egen cell_year_count_B =sum(n) // by year 
{txt}
{com}. drop n  
{txt}
{com}. 
. sum cell_* // outcomes at cell-level 

{txt}    Variable {c |}        Obs        Mean    Std. dev.       Min        Max
{hline 13}{c +}{hline 57}
{space 2}cell_sales {c |}{res}    504,599      132411    251335.4          0    1182962
{txt}cell_year_~s {c |}{res}    504,599     23654.5    51216.35          0   362794.7
{txt}cell_year_~t {c |}{res}    504,599    27.33176    29.42796          0        132
{txt}{space 2}cell_count {c |}{res}    504,599    43.04948    42.29647          0        166
{txt}cell_yea~s_R {c |}{res}     73,795    3642.027    8655.003          0    71978.5
{txt}{hline 13}{c +}{hline 57}
cell_yea~s_B {c |}{res}     69,173    23944.01    40889.19          0   189932.3
{txt}cell_yea~t_R {c |}{res}    504,599    .9746412    3.439936          0         25
{txt}cell_yea~t_B {c |}{res}    504,599     4.53835    16.13203          0        127
{txt}
{com}. 
. sum cell e_low* e_up* if a_bin == 6 & s_bin == 0 // 189, 192 at left - 203, 206 at right 

{txt}    Variable {c |}        Obs        Mean    Std. dev.       Min        Max
{hline 13}{c +}{hline 57}
{space 8}cell {c |}{res}      4,204        1420           0       1420       1420
{txt}{space 2}e_low_left {c |}{res}      4,204    189.9505           0   189.9505   189.9505
{txt}{space 1}e_low_right {c |}{res}      4,204    203.8155           0   203.8155   203.8155
{txt}{space 3}e_up_left {c |}{res}      4,204    192.9505           0   192.9505   192.9505
{txt}{space 2}e_up_right {c |}{res}      4,204    206.8155           0   206.8155   206.8155
{txt}
{com}. 
.         **************  
.         ** monthly ***
.         **************
. 
.         ** cell-level sales, country specific
. by cell date ccode, sort: egen cell_date_sales=sum(units) // by date 
{txt}
{com}. 
.         ** cell-level product count 
. by cell date ccode id,  sort: gen n=_n  
{txt}
{com}. replace n=0                     if n>1 &n!=.  
{txt}(0 real changes made)

{com}. replace n=0 if added_a == 1
{txt}(4,597 real changes made)

{com}. replace n=0 if added_s == 1 
{txt}(0 real changes made)

{com}. by cell date ccode,             sort: egen cell_date_count=sum(n) // by date 
{txt}
{com}. drop n  
{txt}
{com}. 
. summ cell_* // looks ok. 

{txt}    Variable {c |}        Obs        Mean    Std. dev.       Min        Max
{hline 13}{c +}{hline 57}
{space 2}cell_sales {c |}{res}    504,599      132411    251335.4          0    1182962
{txt}cell_year_~s {c |}{res}    504,599     23654.5    51216.35          0   362794.7
{txt}cell_year_~t {c |}{res}    504,599    27.33176    29.42796          0        132
{txt}{space 2}cell_count {c |}{res}    504,599    43.04948    42.29647          0        166
{txt}cell_yea~s_R {c |}{res}     73,795    3642.027    8655.003          0    71978.5
{txt}{hline 13}{c +}{hline 57}
cell_yea~s_B {c |}{res}     69,173    23944.01    40889.19          0   189932.3
{txt}cell_yea~t_R {c |}{res}    504,599    .9746412    3.439936          0         25
{txt}cell_yea~t_B {c |}{res}    504,599     4.53835    16.13203          0        127
{txt}cell_date_~s {c |}{res}    504,599    2042.457    4510.262         -1   43985.88
{txt}cell_date_~t {c |}{res}    504,599     23.5427    25.28376          0        114
{txt}
{com}. 
. 
. *** replace missings for cells added in grid expansion 
.         
. foreach var of varlist price_all {c -(}
{txt}  2{com}. replace `var' = . if added_a == 1 
{txt}  3{com}. {c )-}
{txt}(0 real changes made)

{com}. 
. replace year = 2017 if added_a == 1 // 2017 is partial, at risk of being dropped. 
{txt}(0 real changes made)

{com}. replace year = 2017 if added_s == 1 
{txt}(0 real changes made)

{com}. replace date = 683 if added_a == 1 // 2017 is partial, at risk of being dropped. 
{txt}(4,597 real changes made)

{com}. replace date = 683 if added_s == 1 
{txt}(0 real changes made)

{com}. 
. 
. foreach var of varlist units cell_year_sales cell_year_count cell_date_sales cell_date_count {c -(}
{txt}  2{com}. replace `var' = 0 if added_a == 1 
{txt}  3{com}. replace `var' = 0 if added_s == 1 
{txt}  4{com}. {c )-}
{txt}(4,597 real changes made)
(0 real changes made)
(0 real changes made)
(0 real changes made)
(0 real changes made)
(0 real changes made)
(0 real changes made)
(0 real changes made)
(0 real changes made)
(0 real changes made)

{com}.         
. *save spatial_2_collapse_s0, replace // intermediate save to explore raw data before collapse 
. 
. 
. ******************************************************
.         ** 3: save and collapse at id-county-level 
. cd "$store"
{res}R:\WSV2\TBu_AKe\Spatial_NEW\Data
{txt}
{com}.     ** collapse #1a: by id year for descriptives  
. gen price2 = price_all
{txt}(173,988 missing values generated)

{com}. 
. preserve
{txt}
{com}. sort year month cell id
{txt}
{com}. gen units2 = units
{txt}(169,395 missing values generated)

{com}. #delimit ;
{txt}delimiter now ;
{com}. collapse (firstnm) id_c cell e_* a_* k_bin eei_bin s_bin s capacity kwh eei class_A wm_labelbin revs_min_2 type_wm_2
>                  (max) cell_year_sales cell_year_count sum_countries 
>                  (sum) units_clean units price_all b r u 
>                  (mean) price2 price price_eur age_country age_xc age_country_reverse, 
>                  by(id ccode year) ;
{res}{txt}
{com}. #delimit cr     
{txt}delimiter now cr
{com}.         ** save, for merging later 
. save spatial_4_sample_s, replace 
{txt}{p 0 4 2}
file {bf}
spatial_4_sample_s.dta{rm}
saved
{p_end}

{com}. restore 
{txt}
{com}. 
.     ** collapse #1b: by id date for estimation sample 
. preserve
{txt}
{com}. sort year month cell id
{txt}
{com}. gen units2 = units
{txt}(169,395 missing values generated)

{com}. #delimit ;
{txt}delimiter now ;
{com}. collapse (firstnm) id_c cell e_* a_* k_bin eei_bin s_bin s capacity kwh eei class_A wm_labelbin revs_min_2 type_wm_2
>                  (max) cell_date_sales cell_date_count cell_year_sales cell_year_count sum_countries 
>                  (sum) units_clean units price_all b r u 
>                  (mean) price2 price price_eur age_country age_xc age_country_reverse, 
>                  by(id ccode date) ;
{res}{txt}
{com}. #delimit cr     
{txt}delimiter now cr
{com}.         ** save, for merging later 
. save spatial_4_sample_mc, replace 
{txt}{p 0 4 2}
file {bf}
spatial_4_sample_mc.dta{rm}
saved
{p_end}

{com}. restore 
{txt}
{com}. 
. 
. *******************************************
. ** Part 2: prepare W_matrix aggregation  **
. *******************************************
. 
.         ** collapse #2: by cell year for W_matrix 
. 
.                 *** yearly *** 
. preserve 
{txt}
{com}. sort cell ccode year // stays the same!!! 
{txt}
{com}. 
. replace ccode = 0 if missing(ccode)
{txt}(4,597 real changes made)

{com}. #delimit ;
{txt}delimiter now ;
{com}. collapse (max) cell_year_sales cell_year_count (min) eei_bin2 
>                  (sum) units price_all b r u  
>                  (mean) e_* a_* k_bin s_bin eei_bin capacity kwh s price2 eei 
>                  (firstnm) added_*, 
>                  by(cell ccode year);
{res}{txt}
{com}.  #delimit cr
{txt}delimiter now cr
{com}. order cell_year*, last 
{txt}
{com}. order price_all price2, last 
{txt}
{com}. order capacity kwh units added_*, last
{txt}
{com}. 
. 
. ******************
. *** clean up data 
. *****************
. 
. fillin cell ccode year // fillin to expand grid structure  
{txt}
{com}. codebook cell //  

{txt}{hline}
{res}cell{right:group(e_bin a_bin)}
{txt}{hline}

{col 19}Type: Numeric ({res}float{txt})

{col 18}Range: [{res}1{txt},{res}4950{txt}]{col 55}Units: {res}1
{col 10}{txt}Unique values: {res}4,950{col 51}{txt}Missing .: {res}0{txt}/{res}554,400

{txt}{col 19}Mean: {res}{ralign 7:2475.5}
{txt}{col 14}Std. dev.: {res}{ralign 7:1428.94}

{txt}{col 12}Percentiles: {col 25}{ralign 7:10%}{ralign 10:25%}{ralign 10:50%}{ralign 10:75%}{ralign 10:90%}
{res}{col 25}{ralign 7:495.5}{ralign 10:    1238}{ralign 10:  2475.5}{ralign 10:    3713}{ralign 10:  4455.5}
{txt}
{com}. label variable _fillin "cell-year_fill" // 
{txt}
{com}. replace added_a = 1 if added_a == . // identifier for added cells 1   
{txt}(540,597 real changes made)

{com}. 
. xtset cell // impose cell structure 

{txt}{col 1}Panel variable: {res}cell{txt} (balanced)

{com}. 
. *** replace missings with zero for outcome, set price missing if zero
. foreach var of varlist cell_year_count cell_year_sales units {c -(}
{txt}  2{com}.         replace `var' = 0 if `var' == . 
{txt}  3{com}.         replace `var' = 0 if added_a == 1 & _fillin == 0 // from collapse structure 
{txt}  4{com}. {c )-}
{txt}(540,597 real changes made)
(0 real changes made)
(540,597 real changes made)
(0 real changes made)
(540,597 real changes made)
(0 real changes made)

{com}. 
. foreach var of varlist price_all price2 {c -(}
{txt}  2{com}.         replace `var' = . if `var' == 0  
{txt}  3{com}. {c )-}
{txt}(5,495 real changes made, 5,495 to missing)
(114 real changes made, 114 to missing)

{com}. 
. *** fix missings in cell address (happens with collapse)
.         ** capacity a 
. foreach var of varlist a_min a_max a_bin eei_bin {c -(}
{txt}  2{com}.         gsort cell +`var'
{txt}  3{com}.         bysort cell: carryforward `var', gen(`var'3)
{txt}  4{com}.         quietly list cell `var' `var'3, clean noobs
{txt}  5{com}. 
.         replace `var' = `var'3 
{txt}  6{com}.         drop `var'3 
{txt}  7{com}. {c )-}
a_min3:  {txt}(540,597 real changes made)
{res}{txt}(540,597 real changes made)
a_max3:  (540,597 real changes made)
{res}{txt}(540,597 real changes made)
a_bin3:  (540,597 real changes made)
{res}{txt}(540,597 real changes made)
eei_bin3:  (30,330 real changes made)
{res}{txt}(30,330 real changes made)

{com}. 
. 
.         ** energy consumption e
. foreach var of varlist e_bin e_low_left e_low_right e_up_left e_up_right k_bin s_bin {c -(}
{txt}  2{com}.         gsort cell +`var'
{txt}  3{com}.         bysort cell: carryforward `var', gen(`var'3)
{txt}  4{com}.         quietly list cell `var' `var'3, clean noobs
{txt}  5{com}. 
.         replace `var' = `var'3 
{txt}  6{com}.         drop `var'3 
{txt}  7{com}. {c )-}
e_bin3:  {txt}(540,597 real changes made)
{res}{txt}(540,597 real changes made)
e_low_left3:  (540,597 real changes made)
{res}{txt}(540,597 real changes made)
e_low_right3:  (540,597 real changes made)
{res}{txt}(540,597 real changes made)
e_up_left3:  (540,597 real changes made)
{res}{txt}(540,597 real changes made)
e_up_right3:  (540,597 real changes made)
{res}{txt}(540,597 real changes made)
k_bin3:  (540,597 real changes made)
{res}{txt}(540,597 real changes made)
s_bin3:  (540,597 real changes made)
{res}{txt}(540,597 real changes made)

{com}. 
. *gen eei_digit = ((e_bin)/(47*a_bin+51.7))*100 // check on aggregation structure   
. *replace eei = round(eei_digit, 0.1) //  
. 
. 
. ******** define B and R by s-bin ************
. 
. gen restricted = 0 
{txt}
{com}. gen bunching = 0 
{txt}
{com}. 
. ** tag bunching cells by distance to compliance line 
. replace restricted = 1 if s_bin >= 0 // incompliant
{txt}(319,200 real changes made)

{com}. replace bunching = 1 if s_bin == -3 // first row in compliance
{txt}(2,800 real changes made)

{com}. replace bunching = 1 if s_bin == -6 // second row in compliance 
{txt}(2,800 real changes made)

{com}. 
. gen space = 0 
{txt}
{com}. replace space = 1 if restricted == 1 // 
{txt}(319,200 real changes made)

{com}. replace space = 2 if bunching == 1 // 
{txt}(5,600 real changes made)

{com}. 
. ********************************************
.         ** order cells by segment 
. 
. rename cell cell0 
{res}{txt}
{com}. gsort +year +ccode -space +k_bin +e_bin // sort order is critical 
{txt}
{com}. by year ccode: gen cell = _n // cells are ordered to consider bunching first, then restricted, then rest. 
{txt}
{com}. 
. **************************
. save spatial_2_collapse_s, replace 
{txt}{p 0 4 2}
file {bf}
spatial_2_collapse_s.dta{rm}
saved
{p_end}

{com}. ****************************
. 
. restore
{txt}
{com}. cd "$store" 
{res}R:\WSV2\TBu_AKe\Spatial_NEW\Data
{txt}
{com}.                 *** monthly  *** 
. preserve 
{txt}
{com}. sort cell ccode year month // sort order is critical  
{txt}
{com}. *gen price2 = price_all
. replace ccode = 0 if missing(ccode)
{txt}(4,597 real changes made)

{com}. #delimit ;
{txt}delimiter now ;
{com}. collapse (max) cell_date_sales cell_date_count (min) eei_bin2 
>                  (sum) units price_all b r u  
>                  (mean) e_* a_* k_bin s_bin eei_bin capacity kwh s price2 eei 
>                  (firstnm) added_* year month, 
>                  by(cell ccode date);
{res}{txt}
{com}.  #delimit cr
{txt}delimiter now cr
{com}. order cell_date*, last 
{txt}
{com}. order price_all price2, last 
{txt}
{com}. order capacity kwh units added_*, last
{txt}
{com}. 
. 
. ******************
. *** clean up data 
. *****************
. 
. fillin cell ccode year month // sort order 
{txt}
{com}. codebook cell //  4950 unique cells 

{txt}{hline}
{res}cell{right:group(e_bin a_bin)}
{txt}{hline}

{col 19}Type: Numeric ({res}float{txt})

{col 18}Range: [{res}1{txt},{res}4950{txt}]{col 55}Units: {res}1
{col 10}{txt}Unique values: {res}4,950{col 51}{txt}Missing .: {res}0{txt}/{res}7,207,200

{txt}{col 19}Mean: {res}{ralign 7:2475.5}
{txt}{col 14}Std. dev.: {res}{ralign 7:1428.94}

{txt}{col 12}Percentiles: {col 25}{ralign 7:10%}{ralign 10:25%}{ralign 10:50%}{ralign 10:75%}{ralign 10:90%}
{res}{col 25}{ralign 7:495.5}{ralign 10:    1238}{ralign 10:  2475.5}{ralign 10:    3713}{ralign 10:  4455.5}
{txt}
{com}. label variable _fillin "cell-month_fill" // 
{txt}
{com}. replace added_a = 1 if added_a == . // tag added cells   
{txt}(7,111,324 real changes made)

{com}. 
. xtset cell // impose spatial structure 

{txt}{col 1}Panel variable: {res}cell{txt} (balanced)

{com}. 
. foreach var of varlist cell_date_count cell_date_sales units {c -(}
{txt}  2{com}.         replace `var' = 0 if `var' == . 
{txt}  3{com}.         replace `var' = 0 if added_a == 1 & _fillin == 0 // from collapse structure 
{txt}  4{com}. {c )-}
{txt}(7,111,324 real changes made)
(0 real changes made)
(7,111,324 real changes made)
(0 real changes made)
(7,111,324 real changes made)
(0 real changes made)

{com}. 
. foreach var of varlist price_all price2 {c -(}
{txt}  2{com}.         replace `var' = . if `var' == 0  
{txt}  3{com}. {c )-}
{txt}(28,199 real changes made, 28,199 to missing)
(4,573 real changes made, 4,573 to missing)

{com}. 
. *** fix missings in cell address (happens with collapse)
. 
.         ** capacity a
. foreach var of varlist a_min a_max a_bin eei_bin {c -(}
{txt}  2{com}.         gsort cell +`var'
{txt}  3{com}.         bysort cell: carryforward `var', gen(`var'3)
{txt}  4{com}.         quietly list cell `var' `var'3, clean noobs
{txt}  5{com}. 
.         replace `var' = `var'3 
{txt}  6{com}.         drop `var'3 
{txt}  7{com}. {c )-}
a_min3:  {txt}(7,111,324 real changes made)
{res}{txt}(7,111,324 real changes made)
a_max3:  (7,111,324 real changes made)
{res}{txt}(7,111,324 real changes made)
a_bin3:  (7,111,324 real changes made)
{res}{txt}(7,111,324 real changes made)
eei_bin3:  (422,689 real changes made)
{res}{txt}(422,689 real changes made)

{com}. 
. 
.         ** energy consumption e 
. foreach var of varlist e_bin e_low_left e_low_right e_up_left e_up_right k_bin s_bin {c -(}
{txt}  2{com}.         gsort cell +`var'
{txt}  3{com}.         bysort cell: carryforward `var', gen(`var'3)
{txt}  4{com}.         quietly list cell `var' `var'3, clean noobs
{txt}  5{com}. 
.         replace `var' = `var'3 
{txt}  6{com}.         drop `var'3 
{txt}  7{com}. {c )-}
e_bin3:  {txt}(7,111,324 real changes made)
{res}{txt}(7,111,324 real changes made)
e_low_left3:  (7,111,324 real changes made)
{res}{txt}(7,111,324 real changes made)
e_low_right3:  (7,111,324 real changes made)
{res}{txt}(7,111,324 real changes made)
e_up_left3:  (7,111,324 real changes made)
{res}{txt}(7,111,324 real changes made)
e_up_right3:  (7,111,324 real changes made)
{res}{txt}(7,111,324 real changes made)
k_bin3:  (7,111,324 real changes made)
{res}{txt}(7,111,324 real changes made)
s_bin3:  (7,111,324 real changes made)
{res}{txt}(7,111,324 real changes made)

{com}. 
. 
. rename date date_cal  // date format and fillin creates error 
{res}{txt}
{com}. gen year_cal = year 
{txt}
{com}. 
. replace year_cal = year - 1 if month == 12
{txt}(554,400 real changes made)

{com}. 
. sort year_cal month
{txt}
{com}. generate date = ym(year_cal, month) // date variable out of year and month
{txt}(554,400 missing values generated)

{com}. format date %tm 
{txt}
{com}. 
. *gen e_max_digit = 0            
. *bysort eei_bin a_bin: replace e_max_digit = ((eei_bin+1)/100)*(47*a_bin+51.7)
. 
. *gen eei_digit = ((e_bin)/(47*a_bin+51.7))*100 // used in robustness check for rounding  
. *replace eei = round(eei_digit, 0.1) // 
. 
. ******** define B and R by s-bin ************
. 
. gen restricted = 0 
{txt}
{com}. gen bunching = 0 
{txt}
{com}. 
. replace restricted = 1 if s_bin >= 0 // incompliant 
{txt}(4,149,600 real changes made)

{com}. replace bunching = 1 if s_bin == -3 // first row in compliance 
{txt}(36,400 real changes made)

{com}. replace bunching = 1 if s_bin == -6 // second row in compliance 
{txt}(36,400 real changes made)

{com}. 
. gen space = 0 
{txt}
{com}. replace space = 1 if restricted == 1 
{txt}(4,149,600 real changes made)

{com}. replace space = 2 if bunching == 1 
{txt}(72,800 real changes made)

{com}. 
. rename cell cell0 
{res}{txt}
{com}. gsort +year +month +ccode -space +k_bin +e_bin // sort order is critical 
{txt}
{com}. by year month ccode: gen cell = _n // cells are ordered to consider bunching first, then restricted then rest. 
{txt}
{com}. 
. **************************
. save spatial_2_collapse_mc, replace 
{txt}{p 0 4 2}
file {bf}
spatial_2_collapse_mc.dta{rm}
saved
{p_end}

{com}. ****************************
. 
. restore 
{txt}
{com}. 
. log close
      {txt}name:  {res}<unnamed>
       {txt}log:  {res}R:\WSV2\TBu_AKe\Spatial_NEW\spatial_2_collapse_s.smcl
  {txt}log type:  {res}smcl
 {txt}closed on:  {res}28 Mar 2023, 16:35:33
{txt}{.-}
{smcl}
{txt}{sf}{ul off}